﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;
using BasicDataManagement;

namespace StockManagement
{
    /// <summary>
    /// PurchaseWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PurchaseWindow : Window
    {
        class MaterialStockInfo
        {
            public string MaterialName { get; set; }
            public string Unit { get; set; }
            public double LeftVaule { get; set; }
            public double AddValue { get; set; }
        }

        IOpertation op;
        public PurchaseWindow()
        {
            InitializeComponent();
            op = OperationFactory.Create();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<MaterialObject> mats = op.LoadMaterialObjects();
            List<MaterialStockInfo> matStockList = new List<MaterialStockInfo>();
            foreach (var t in mats)
            {
                List<StockRecordItem> tmpSRD = op.LoadMaterialStockRecordsMaterial(t.ObjectName);
                double leftV = 0.0;
                foreach (var s in tmpSRD)
                {
                    if (s.StockDir == StockDirection.PutIn)
                    {
                        leftV += s.Value;
                        continue;
                    }
                    else
                    {
                        leftV -= s.Value;
                    }
                }
                matStockList.Add(new MaterialStockInfo()
                {
                    AddValue = 0.0,
                    LeftVaule = leftV,
                    MaterialName = t.ObjectName,
                    Unit = t.Unit
                });
            }
            dataGridPurchase.ItemsSource = matStockList;
        }

        private void btnSaveAdd_Click(object sender, RoutedEventArgs e)
        {
            if (reasonTB.Text.Equals(""))
            {
                MessageBox.Show("请填写入库原因");
                return;
            }
            List<MaterialStockInfo> matStockList = dataGridPurchase.ItemsSource as List<MaterialStockInfo>;
            foreach (var t in matStockList)
            {
                if (t.AddValue == 0)
                {
                    continue;
                }
                op.AddMaterialStockRecord(new StockRecordItem()
                {
                    MaterialName = t.MaterialName,
                    RecTime = DateTime.Now,
                    StockDir = StockDirection.PutIn,
                    Reason = reasonTB.Text,
                    Value = t.AddValue
                });
            }
            MessageBox.Show("入库成功！");
            this.Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
